<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="报告日期" prop="bgrq">
        <el-date-picker clearable size="small" v-model="queryParams.bgrq" type="date" value-format="yyyy-MM-dd" placeholder="选择报告日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="报表编号" prop="bbbh">
        <el-input v-model="queryParams.bbbh" placeholder="请输入报表编号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['Laboratory_LYS:LYS_Main:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['Laboratory_LYS:LYS_Main:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['Laboratory_LYS:LYS_Main:remove']">删除</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['Laboratory_LYS:LYS_Main:export']">导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="LYS_MainList" @selection-change="handleSelectionChange" max-height="550">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报表编号" align="center" prop="id" />
      <el-table-column label="报告日期" align="center" prop="bgrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bgrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报表编号" align="center" prop="bbbh" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="excelhandleView(scope.row)"
          >导出</el-button            >
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['Laboratory_LYS:LYS_Main:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['Laboratory_LYS:LYS_Main:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改留样室产品检验记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body fullscreen>
      <TableHeader @nameChange="nameChange" :data="{
          mainTitle: '留样室产品检验记录',
          company: '青岛崂山矿泉水有限公司',
          code: form.bbbh,
        }" />
      <br />
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="报告日期" prop="bgrq">
              <el-date-picker clearable size="small" v-model="form.bgrq" type="date" value-format="yyyy-MM-dd" placeholder="选择报告日期" :disabled="openDisabled">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
                <el-form-item label="报表编号" prop="bbbh">
                  <el-input v-model="form.bbbh" placeholder="请输入报表编号" :disabled="openDisabled" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="备注" prop="remark">
                  <el-input v-model="form.remark" placeholder="请输入备注" :disabled="openDisabled" />
                </el-form-item>
            </el-col> -->
        </el-row>
        <el-divider content-position="center">留样室产品检验记录子信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddQualityLabLysSub" :disabled="openDisabled">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteQualityLabLysSub" :disabled="openDisabled">删除</el-button>
          </el-col>
        </el-row>
        <el-table max-height="550" :data="qualityLabLysSubList" :row-class-name="rowQualityLabLysSubIndex" @selection-change="handleQualityLabLysSubSelectionChange" ref="qualityLabLysSub">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="样品名称" prop="ypmc" width="320">
            <template slot-scope="scope">
              <!-- <el-input v-model="scope.row.ypmc" placeholder="请输入样品名称" :disabled="openDisabled"/> -->
              <Dialog @dialogChange="
                  qualityLabLysSubList[scope.$index].ypmc = $event.label
                " :echoValue="scope.row.ypmc" parameter="rows" value="id" label="name" :multiple="false" search="name/code" url="/basic/productInfo/listSelect" :column="['产成品编号/code', '产成品名称/name']" placeholder="请选择内容" :axiosData="{ typeCode: 'YYTRKQS' }" :openDisabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="产品编号" prop="cpbh" width="170">
            <template slot-scope="scope">
              <el-input v-model="scope.row.cpbh" placeholder="请输入产品编号" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="气味分析" prop="qwfx" width="170">
            <template slot-scope="scope">
              <el-input type="textarea" v-model="scope.row.qwfx" placeholder="请输入气味分析" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="滋味分析" prop="zwfx" width="170">
            <template slot-scope="scope">
              <el-input type="textarea" v-model="scope.row.zwfx" placeholder="请输入滋味分析" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="zt" width="170">
            <template slot-scope="scope">
              <el-input type="textarea" v-model="scope.row.zt" placeholder="请输入状态" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="色度" prop="sd" width="170">
            <template slot-scope="scope">
              <el-input type="textarea" v-model="scope.row.sd" placeholder="请输入色度" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="浊度" prop="zd" width="170">
            <template slot-scope="scope">
              <el-input type="textarea" v-model="scope.row.zd" placeholder="请输入浊度" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="检验结论" prop="jyjl" width="170">
            <template slot-scope="scope">
              <el-input type="textarea" v-model="scope.row.jyjl" placeholder="请输入检验结论" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="检验时间" prop="jysj" width="150px">
            <template slot-scope="scope">
              <el-date-picker clearable size="small" v-model="scope.row.jysj" type="date" value-format="yyyy-MM-dd" placeholder="选择检验时间" :disabled="openDisabled">
              </el-date-picker>
            </template>
          </el-table-column>
          <!-- <el-table-column label="检验人" prop="jyr">
            <template slot-scope="scope">
                <el-input v-model="scope.row.jyr" placeholder="请输入检验人" :disabled="openDisabled"/>
            </template>
          </el-table-column>
          <el-table-column label="复核人" prop="fhr">
            <template slot-scope="scope">
                <el-input v-model="scope.row.fhr" placeholder="请输入复核人" :disabled="openDisabled"/>
            </template>
          </el-table-column> -->

          <el-table-column label="检验人" prop="jyr" width="200">
            <template slot-scope="scope">
              <!-- <el-input
                v-model="scope.row.jyr"
                placeholder="请输入检验人"
                :disabled="openDisabled"
              /> -->
              <el-select v-model="scope.row.jyr" filterable allow-create placeholder="请选择检验人" style="width: 100%" :disabled="openDisabled">
                <el-option v-for="item in userList" :key="item.nickName" :label="item.nickName" :value="item.nickName" :disabled="openDisabled"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="复核人" prop="fhr" width="200">
            <template slot-scope="scope">
              <el-select v-model="scope.row.fhr" filterable allow-create placeholder="请选择复核人" style="width: 100%" :disabled="openDisabled">
                <el-option v-for="item in userList" :key="item.nickName" :label="item.nickName" :value="item.nickName" :disabled="openDisabled"></el-option>
              </el-select>

              <!-- <el-input
                v-model="scope.row.fhr"

                :disabled="true"
              /> -->
            </template>
          </el-table-column>

          <!-- <el-table-column label="报表编号" prop="bbbh">
            <template slot-scope="scope">
                <el-input v-model="scope.row.bbbh" placeholder="请输入报表编号" :disabled="openDisabled"/>
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
                <el-input v-model="scope.row.remark" placeholder="请输入备注" :disabled="openDisabled"/>
            </template>
          </el-table-column> -->
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLYS_Main,
  getLYS_Main,
  delLYS_Main,
  addLYS_Main,
  updateLYS_Main,
} from "@/api/quality/laboratory/LYS_Main";
import { listUser } from "@/api/system/user";
import { getInfo } from "@/api/login";
export default {
  name: "LYS_Main",
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedQualityLabLysSub: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 留样室产品检验记录表格数据
      LYS_MainList: [],
      // 留样室产品检验记录子表格数据
      qualityLabLysSubList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        bgrq: null,
        bbbh: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      /* 当前用户名 */
      nickName: "",
      userList: [],
    };
  },
  created() {
    this.getList();
    this.queryUserList();
  },
  methods: {
    /** 查询留样室产品检验记录列表 */
    getList() {
      this.loading = true;
      listLYS_Main(this.queryParams).then((response) => {
        this.LYS_MainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bgrq: null,
        bbbh: "LK-QF-LS1-10-015-11-05",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.qualityLabLysSubList = [];
      this.resetForm("form");
    },
    //接收编号
    nameChange(e) {
      this.form.bbbh = e;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加留样室产品检验记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getLYS_Main(id).then((response) => {
        this.form = response.data;
        this.qualityLabLysSubList = response.data.qualityLabLysSubList;
        this.open = true;
        this.title = "修改留样室产品检验记录";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getLYS_Main(id).then((response) => {
        this.form = response.data;
        this.qualityLabLysSubList = response.data.qualityLabLysSubList;
        this.open = true;
        this.title = "查看留样室产品检验记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.qualityLabLysSubList = this.qualityLabLysSubList;
          if (this.form.id != null) {
            updateLYS_Main(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLYS_Main(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除留样室产品检验记录编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delLYS_Main(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 留样室产品检验记录子序号 */
    rowQualityLabLysSubIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 留样室产品检验记录子添加按钮操作 */
    handleAddQualityLabLysSub() {
      let obj = {};
      obj.ypmc = "";
      obj.cpbh = "";
      obj.qwfx = "";
      obj.zwfx = "";
      obj.zt = "";
      obj.sd = "";
      obj.zd = "";
      obj.jyjl = "";
      obj.jysj = "";
      obj.jyr = "";
      obj.fhr = "";
      obj.bbbh = "";
      obj.remark = "";
      this.qualityLabLysSubList.push(obj);
    },
    /** 留样室产品检验记录子删除按钮操作 */
    handleDeleteQualityLabLysSub() {
      if (this.checkedQualityLabLysSub.length == 0) {
        this.$alert("请先选择要删除的留样室产品检验记录子数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.qualityLabLysSubList.splice(
          this.checkedQualityLabLysSub[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleQualityLabLysSubSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.qualityLabLysSub.clearSelection();
        this.$refs.qualityLabLysSub.toggleRowSelection(selection.pop());
      } else {
        this.checkedQualityLabLysSub = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "Laboratory_LYS/LYS_Main/export",
        {
          ...this.queryParams,
        },
        "留样室产品检验记录" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    excelhandleView(row){
      this.download(
        "Laboratory/LYS_Main/exportexcel?id="+row.id,
        {
          ...this.queryParams,
        },
        "仰口基地留样室产品检验记录" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    /* 查询用户列表 */
    queryUserList() {
      listUser().then((response) => {
        this.userList = response.rows;
      });
      getInfo().then((response) => {
        this.nickName = response.user.nickName;
      });
    },
  },
};
</script>
